from django.db import DatabaseError
from django.http import HttpResponseBadRequest

from apps.index.models import Today, EverydaySentence, Notification


def hanhan():
    try:
        today = Today.objects.get(id=1)
    except DatabaseError:
        raise('发生了预期之外的错误')

    content = today.content[0:120] + '...'

    # read tb_EverydaySentence from db
    try:
        everyday = EverydaySentence.objects.get(id=1)
        date_dict = str(everyday.date).split('-')
        Evecontent = everyday.content
        Evecontent1 = everyday.content2
        # print(type(date))
    except DatabaseError:
        raise('发生了预期之外的错误')
    date = ''
    for num, item in enumerate(date_dict):
        if num == 0:
            item = item + "年"
        elif num == 1:
            item = item + '月'
        else:
            item = item + '日'
        date += item
    print(date)

    # read tb_Notification from db
    try:
        notifications = Notification.objects.filter(id__range=[1, 6])
    except  DatabaseError:
        raise ('发生了预期之外对错误')

    context = {

        'title': today.title,
        'content': content,
        "date": date,
        'Evecontent': Evecontent,
        'Evecontent1': Evecontent1,
        'notifications': notifications,
    }

    return  context